<script setup>
import {ref} from 'vue'
import { defineProps ,onMounted,reactive} from 'vue';
import {getUserInfo} from "../api/index";
import router from "@/router";
// 获取父组件的参数
const {showCollapse,isCollapse}=defineProps(['showCollapse','isCollapse'])
// 鼠标移到用户头像时显示用户信息
const show = ref(false)
const showUserInfo = (type) => {
  type === 'show' ? show.value = true : show.value = false
}

// 获取用户信息接口
const userInfo=reactive({
  name:'',
  headerImg:''
})
// 用户信息接口调用
const getUserInfoData=async()=>{
  const res=await getUserInfo()
  if(res.data?.name&&res.data?.head_img){
    userInfo.name=res.data.name
    userInfo.headerImg=res.data.head_img
    console.log('1111',res)
  }
}
onMounted(()=>{
  getUserInfoData()
})

const goLogin=()=>{
  router.push('/login')
  localStorage.removeItem('token')
}
</script>

<template>
<div class="header">
  <div class="header_ico">
    <el-icon size="30px" @click="showCollapse" :style="{transform:isCollapse?'rotate(180deg)':''}"><Expand /></el-icon>
    <span>首页</span>
  </div>
  <div class="user"
  @mouseenter="showUserInfo('show')"
       @mouseleave="showUserInfo('leave')"
  >
    <img :src="userInfo.headerImg">
    <div class="userInfo" v-show="show">
      <div>{{userInfo.name}}</div>
      <div @click="goLogin">退出登录</div>
    </div>
  </div>
</div>
</template>

<style lang="less" scoped>
.header{
  width: 100%;
  height: 100%;
  display:flex;
  justify-content: space-between;

}
.header_ico{
  display: flex;
  align-items: center;
  span{
    margin-left: 18px;
  }
}
.user {
  display: flex;
  justify-content: center;
  width: 80px;

  img {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    margin-top: 5px;
  }
}
.userInfo{
  padding: 8px;
  margin: 0 0 0 10px;
  z-index: 22;
  display: flex;
  flex-direction: column;
  position: absolute;
  right:20px;
  top: 55px;
  background-color: white;
  border: 5px;
  box-shadow: 0 4px 8px 0 rgb(7 17 27 / 10%);
  text-align: center;
  div{
    margin: 5px 0;
  }
  div:hover{
    color: #409eff;
  }
}
</style>